# vim: set fileencoding=utf-8 :
# -*- coding: utf-8 -*-

import sys

import requests #导入requests包
import json
from bs4 import BeautifulSoup #从bs4导入beautifulsoup包
from fake_useragent import UserAgent

import json
import time


class http(object):

    # 获取指定 url 的 html 内容，编码为 utf-8
    def get_html(url, httpHeads=None):
        ua = UserAgent()
        headers = ua.random
        heads = {}
        heads['User-Agent'] = headers
        if httpHeads != None:
            for h in httpHeads:
                heads[h] = httpHeads[h]
        response = requests.get(url, headers=heads)  # 从链接获取所有的网页源码
        if response.status_code == 200:
            response.encoding = response.apparent_encoding  # ='gb2312'#  'utf-8'#转化编码模式为utf-8
            return response.text  # 返回转化之后的源码
        else:
            raise Exception(-1, "%s 连接失败" % (url))

            # 获取指定 url 的 html 内容，编码为utf-8

    def __post_html(url, data, contentType="application/x-www-form-urlencoded", httpHeads=None):
        # print("post："+url);
        jsonData = json.dumps(data)
        # print("postData："+jsonData);
        ua = UserAgent()
        headers = ua.random
        heads = {}
        heads['User-Agent'] = headers
        heads['Content-type'] = contentType;
        if httpHeads != None:
            for h in httpHeads:
                heads[h] = httpHeads[h]
        # print (heads)

        response = requests.post(url, data=jsonData, headers=heads)  # 从链接获取所有的网页源码
        if response.status_code == 200:
            response.encoding = response.apparent_encoding  # ='gb2312'#  'utf-8'#转化编码模式为utf-8
            return response.text  # 返回转化之后的源码
        else:
            raise Exception(-1, "%s 连接失败" % (url))

            # 获取 html 中的指定标签

    def __get_certain_joke(html, flag):
        """get the joke of the html"""
        try:
            soup = BeautifulSoup(html, 'lxml')  # 使用lxml解析器对网页进行解析（可以使用默认解析器，但是lxml解析器功能更加强大）
            # list=soup.select('span[class="city"]')
            list = soup.select(flag)
            return list
        except(Exception):
            return None


    """description of class"""
    def get(url,fname=None,httpHeads=None):
        time_start=time.time()
        res=http.get_html(url,httpHeads);
        time_end=time.time()
        if fname!=None:
            print(fname,"%.3f" %(time_end-time_start))
        jsonData=json.loads(res);
        return jsonData

    def post(url,data,fname=None,httpHeads=None,contentType="application/json;utf-8;"):
        time_start=time.time()
        res=http.__post_html(url,data,contentType,httpHeads);

        time_end=time.time()
        if fname!=None:
            print(fname,"%.3f" %(time_end-time_start))
        jsonData=json.loads(res);        
        return jsonData

